package com.util;


import com.dao.NewsDao;
import com.dao.impl.NewsDaoimpl;
import com.entity.News;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JsoupUtil {
  static NewsDao js = new NewsDaoimpl();
   static Element team1[] = new Element[100];
   static int count =0;
     static int start=0;
    public static void getDate() {
        Document document = null;
        try {
            document = Jsoup.connect("https://home.meishichina.com/blog/page/2/").get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<News> list = new ArrayList<>();
        Elements select = document.getElementsByClass("ui_newlist_1");
        for (Element element : select) {
            Elements select1 = element.select("li>div>h2>a");
            for (Element element1 : select1) {
                System.out.println(element1.text()+"        "+element1.attr("href"));
                News news = null;
                try {
                    news = getNews(element1.text(), element1.attr("href"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                js.insertDate(news);
            }
        }
    }
    public static News getNews(String title,String url) throws IOException {
        News news = new News();
        news.setTitle(title);
        Document document = Jsoup.connect(url).get();
        Element content = document.getElementsByClass("blog_message").first();
        if(content!=null){
            news.setContent(content.html());
            Elements select = document.getElementsByClass("rtime");
            news.setTime(select.text());
            String time = news.getTime().substring(3,news.getTime().length()-1);
            news.setTime(time);
            Element select1 = document.getElementById("blog_username");
            news.setAuthor(select1.text());
            Element select2 =  document.getElementsByClass("blog_message").first();
            Element img = select2.select("img[src]").first();
            news.setImage(img.attr("src"));


            if(count==0){
                Document document1 = Jsoup.connect("https://home.meishichina.com/blog/page/2/").get();
                //内容截取
                Elements subcontent = document1.getElementsByClass("subcontent");
                Element team[] = new Element[subcontent.size()];
                for(int i=0;i<subcontent.size();i++){
                    team[i] = subcontent.get(i);
                    team1[i] = team[i];
                }
            }
            for (Element element: team1) {
                news.setSubcontent(team1[start].text());
                break;
            }
            start++;
            count++;
        }
        return news;
    }

    public static void main(String[] args) {
        getDate();
    }
}

